package com.funny.learning.english.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.funny.learning.english.R;
import com.funny.learning.english.adapter.HistoryAdapter;
import com.funny.learning.english.db.HistoryDAO;
import com.funny.learning.english.model.HistoryEntity;

public class TestHisotyActivity extends Activity {

	private Spinner mSpTopic;
	private Spinner mSpSubTopic;

	private String mTopic = null;
	private String mSubTopic = null;

	private ArrayList<String> mTopics = new ArrayList<String>();
	private ArrayList<String> mSubTopics = new ArrayList<String>();
	private ArrayList<HistoryEntity> mHistoryEntities = new ArrayList<HistoryEntity>();

	private ArrayAdapter<String> mSpSubTopicArrayAdapter;

	private HistoryDAO mHistoryDAO;

	private ListView mLstHistory;
	private HistoryAdapter mHistoryAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_historytest);

		mHistoryDAO = new HistoryDAO(this);

		mSpTopic = (Spinner) this.findViewById(R.id.spTopic);
		mSpSubTopic = (Spinner) this.findViewById(R.id.spSubtopic);
		mLstHistory = (ListView) this.findViewById(R.id.lstHistory);

		mTopics.addAll(mHistoryDAO.getAllTopic());

		ArrayAdapter<String> mSpTopicArrayAdapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, mTopics);
		mSpTopicArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpTopic.setAdapter(mSpTopicArrayAdapter);
		mSpTopic.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mTopic = (String) parent.getItemAtPosition(position);
				 
				mSubTopics.clear();
				mSubTopics.addAll(mHistoryDAO.getAllSubtopicByTopicName(mTopic));
				mSpSubTopicArrayAdapter.notifyDataSetChanged();
				
				if(!mSubTopics.isEmpty())
					mSubTopic = mSubTopics.get(0);
				
				onItemKICK(); 
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		if (!mTopics.isEmpty()) {
			mTopic = mTopics.get(0);
			mSubTopics.addAll(mHistoryDAO.getAllSubtopicByTopicName(mTopic));

			mSpSubTopicArrayAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, mSubTopics);
			mSpSubTopicArrayAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			mSpSubTopic.setAdapter(mSpSubTopicArrayAdapter);
			mSpSubTopic.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					mSubTopic = (String) parent.getItemAtPosition(position);
					onItemKICK();
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					// TODO Auto-generated method stub

				}
			});
		}

		if (!mTopics.isEmpty() && !mSubTopics.isEmpty()) {
			mSubTopic = mSubTopics.get(0);

			mHistoryEntities
					.addAll(mHistoryDAO
							.getAllHistoryByTopicNameAndSubtopicName(mTopic,
									mSubTopic));

		}

		mHistoryAdapter = new HistoryAdapter(this, mHistoryEntities);
		mLstHistory.setAdapter(mHistoryAdapter);

		// test save
		// HistoryEntity historyEntity = new HistoryEntity();
		// historyEntity.setTime((int) System.currentTimeMillis());
		// historyEntity.setDung_count(7);
		// historyEntity.setTong_count(10);
		// historyEntity.setGame("Đánh nhau");
		// historyEntity.setThumb("xyz.jpg");
		// historyEntity.setTopic("xxxx");
		// historyEntity.setSubtopic("xyz");
		// mHistoryDAO.saveHistory(historyEntity);
	}

	private void onItemKICK() {
		if (!TextUtils.isEmpty(mSubTopic) && !TextUtils.isEmpty(mTopic)) {
			mHistoryEntities.clear();
			mHistoryEntities
					.addAll(mHistoryDAO
							.getAllHistoryByTopicNameAndSubtopicName(mTopic,
									mSubTopic));
			mHistoryAdapter.notifyDataSetChanged();
		}
	}

}
